<!DOCTYPE html>
<html>
	<head th:include="/common/head :: head">
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit|ie-comp|ie-stand">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<meta http-equiv="Cache-Control" content="no-siteapp" />
	</head>
<body>
	<div class="x-body">
		<form class="layui-form" lay-filter="add_form">
			<div class="layui-form-item">
				<label for="userId" class="layui-form-label">员工姓名</label>
				<div class="layui-input-inline">
					<select  id="userId" name="userId" lay-search="" lay-verify="userId">
						<option value=""></option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label for="level" class="layui-form-label">绩效等级</label>
				<div class="layui-input-inline">
				 	<button id="plus"  class="layui-btn  layui-btn-sm ">+</button>
					<input type="text" class="layui-btn-sm" size="2" id="level" name="level" value='+0'/>
					<button id="minus" class="layui-btn  layui-btn-sm ">-</button>
				</div>
			</div>
			
			<div class="layui-form-item layui-form-text">
              	<label for="remarks" class="layui-form-label">备注信息
				</label>
              <div class="layui-input-block">
                  <textarea  id="remarks" name="remarks" autocomplete="off" class="layui-textarea"></textarea>
              </div>
          </div>
			<div class="layui-form-item">
				<label for="L_repass" class="layui-form-label"> </label>
				<button class="layui-btn" lay-filter="add_form" lay-submit="">添加</button>
			</div>
		</form>
	</div>
</body>
<script>
	var addFormData={}
	$(function(){
		//获取用户姓名、id下拉表
		getUserId();
		//初始化添加表单
		initAddForm();
		//绑定事件
  	  	bindEvent();
	})
	function bindEvent(){
		//绩效增加
		$("#plus").bind("click",function(){
			var level= $("#level").val();
			var num =parseInt(level)+1;
			$("#level").val("");
			if(num>=0){
				$("#level").val("+"+num);
			}else{
				$("#level").val(num);
			}
			return false;
		})
		//绩效减少
		$("#minus").bind("click",function(){
			var level= $("#level").val();
			var num =parseInt(level)-1;
			$("#level").val("");
			if(num>=0){
				$("#level").val("+"+num);
			}else{
				$("#level").val(num);
			}
			return false;
		})
	}
	
	//初始化添加表单
	function initAddForm(){
		layui.use(['form','layer'],function(){
			$ = layui.jquery;
			var form = layui.form,
			layer = layui.layer;
			//验证规则
			form.verify({
				userId:function(value){
					if(value=="" || value==null){
						return '必填项不能为空';
					}
				}
			});
			//监听提交 
			form.on('submit(add_form)',function(data){
				console.log(data);
				//console.log(data.field);
				addFormData = data.field;
				addMent();
				return false;
			});
		});
	}
	//添加计划
	function addMent(){
			$.ajax({
				url:"/achievement/addMent",
				type:"post",
				data:{
					"userId":addFormData.userId,
					"level":addFormData.level,
					"remarks":addFormData.remarks
				},
				success:function(result){
					console.log(JSON.stringify(result));
					if(result.status==0){
						layer.msg(result.msg,function(){
							//关闭弹出层
							var index = parent.layer.getFrameIndex(window.name);
						    parent.layer.close(index);
						    //刷新当前页面
							//parent.location.replace(parent.location.href);
	                    });
					}else{
						layer.msg(result.msg);
					}
				}
			})
	}
	//异步改为同步  async:false;
	//获取用户姓名、id列表
	function getUserId(){
		$.ajax({
			url:"/user/getUserIds",
			type:"post",
			async:false,
			success:function(result){
				if(result.status==0){
					showUserName(result);	
				}else{
					layer.msg(result.msg);
				}
			}	
		})
	}
	//展示下拉框
	function showUserName(result){
		var str= "";
		var list = result.data;
		for(i in list){
			str+="<option  value="+list[i].id+">"+list[i].userName+"</option>";
		}
		$("#userId").append(str);
	}
	
	
</script>
</html>